/*
 * 常用公共函数
*/
#ifndef COMMON_H
#define COMMON_H

#include "stdint.h"

/*! @name Min/max macros */
/* @{ */
#if !defined(MIN)
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif

#if !defined(MAX)
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif

#if !defined(KHZ)
#define KHZ(a) (a*1000ul)
#endif

#if !defined(MHZ)
#define MHZ(a) (a*1000000ul)
#endif

typedef enum
{
    kDebSignal_StatusStable = 0,
    kDebSignal_StatusShake = 1,
}kDebSignal_Status_t;

/* 带去抖动功能的信号 */
typedef struct
{
    uint16_t interval; //去抖动间隔, mS
    uint16_t cnt; //去抖动计时, mS
    uint16_t status:2; //状态
    uint16_t current_value:1; //当前信号
}debouncing_signal_t;

extern void debouncing_signal_init(debouncing_signal_t *signal, uint16_t interval, uint8_t initial_value);
extern uint8_t debouncing_signal_in(debouncing_signal_t *signal, uint16_t tick, uint8_t s_in);


/*! @} */
typedef enum
{
    kRingBuffer_status_empty = 0,
    kRingBuffer_status_normal = 1,
    kRingBuffer_status_full = 2,
    kRingBuffer_status_overflow = 3,
}ring_buffer_status_t;

typedef struct
{
    uint8_t *buff;
    uint16_t size;
    volatile uint32_t head;
    volatile uint32_t tail;
    uint8_t status;
}ring_buffer_t;

/*! @brief data transfer structure. */
typedef struct
{
    uint8_t *data;   /*!< The buffer of data to be transfer.*/
    uint32_t dataSize; /*!< The byte count to be transfer. */
}transfer_t;

extern void ring_buffer_init(ring_buffer_t *buffer, uint8_t *buff, uint16_t size);
extern void ring_buffer_write(ring_buffer_t *buffer, uint8_t data);
extern uint32_t ring_buffer_get_length(ring_buffer_t *buffer);
extern uint32_t ring_buff_read(ring_buffer_t *buffer, uint8_t *read_buffer, uint16_t read_size);

extern uint8_t check8BitSum(uint8_t *p_dat, uint16_t len);
#endif

